#!/bin/bash
##################################
source ~/scriptbelt/bashutilities.sh
##################################

trial=0
cluster=$1
qcdorder=todk
next=0
fetch=true
finalcluster=$(( $cluster + $next ))
source=lxplus

if [ -z "$cluster" ]; then
	exit
fi

for iarg in $*; do
    CLIinputs[$argCounter]=$iarg
    

    option=`echo "$iarg" | cut -d"=" -f1`
	if [ "$option" == "--writecommand" ]; then
	fetch=false
	#index=`echo "$iarg" | cut -d"=" -f2`
	echo -e "will write the command to be given to fetch and merge multiple runs"
    fi
    if [ "$option" == "--trial" ]; then
	trial=`echo "$iarg" | cut -d"=" -f2`
	echo -e "will fetch the run " $trial "of the cluster "$1
    fi
    if [ "$option" == "--source" ]; then
	source=`echo "$iarg" | cut -d"=" -f2`
	echo -e "will fetch the run from " $source
    fi
    if [ "$option" == "--qcdorder" ]; then
	
	qcdorder=`echo "$iarg" | cut -d"=" -f2`
	echo -e "will fetch the run " $qcdorder "of the cluster "$1
    fi
    if [ "$option" == "--next" ]; then
	
	next=`echo "$iarg" | cut -d"=" -f2`
	finalcluster=$(( $cluster + $next ))
	echo -e "will fetch the runs from cluster "$cluster" to "$finalcluster
    fi
    if [ "$option" == "-h" ]; then
	
	echo "available options:"
	echo "--next (default is --next=0 that is just the run specified in the CLI)"
	echo "--qcdorder (default is --qcdorder=todk)"
	echo "--trial: which of the iterations to fetch (default is --trial=0)"
	echo "--source: lxplus or umd or pisa  (default is lxplus)"
	echo "example call for import: mcfm_import_run_gnu.sh 125872 --source=lxplus --next=0 --trial='' --qcdorder=todk_multiR"
	exit
    fi
done


    if [ $source = umd ]; then
            server="rfrances@hepcms-in1.umd.edu:"
            mcfmrunfolder=/home/rfrances/public_html/mcfm6.8/MCFM-6.8-Condor/tt_"$qcdorder"_production/
    fi
    if [ $source = lxplus ]; then
            server="rfrances@lxplus.cern.ch:"
            mcfmrunfolder=/afs/cern.ch/user/r/rfrances/public_html/lpcc/MCFM-6.8/tt_"$qcdorder"_production/
    fi
    if [ $source = pisa ]; then
            server="rfrances@localui3.pi.infn.it:"
            mcfmrunfolder=/gpfs/ddn/users/rfrances/MCFM-6.8/tt_"$qcdorder"_production/
    fi


currentcluster=$cluster
if [ $fetch = true ]; then
	for ((i=0; i < $(( $next + 1)) ; ++i)); do 
		echo "fetching  $i"
		currentcluster=$(( $cluster + $i ))
			echo "will transfer the trial "$trial" of cluster "$currentcluster" from "$mcfmrunfolder
			scp "$server""$mcfmrunfolder""*"$currentcluster"_"$trial"*.gnu" .
			#find $currentcluster
			echo "the following files will be removed because are size 0"
			#find . -iname "tt_b*$currentcluster*.gnu" -a -size 0 -maxdepth 1 -exec ls {} \;
			find . -iname "tt_b*$currentcluster*.gnu" -a -size 0 -maxdepth 1 -exec rm -v {} \;
	#        oneliner="cp -v ""$mcfmrunfolder"'*'$currentcluster'_'$trial'* '$mcfmrunfolder"transfered/;" 
	#        echo -e $RED"$oneliner"$COL_RESET
	#        ssh  rfrances@hepcms-in1.umd.edu "$oneliner"
	done
fi

globalcommand=""


if [ $fetch = false ]; then

	echo $cluster | sed -n 1'p' | tr ',' '\n' | while read clusterid; do
		echo "adding the command for the run "$clusterid;
		globalcommand+=`echo "mcfm_import_run_gnu.sh "$clusterid" --source="$source" --next="$next" --trial='' --qcdorder=""$qcdorder"";"" MCFM_countruns.sh --merge --id="$clusterid" --times="$(( $next + 1 ))"; "`
		echo "the result is"
		echo -e $RED"$globalcommand"$COL_RESET		
	done

fi

